package 京东笔试;

import java.util.Scanner;

/**
* @author: fujiangnan
* @date: 2021/10/30
* @description: 京东笔试
**/
public class Main1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();
        int[] arr = new int[len];
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < len; i++) {
            arr[i] = sc.nextInt();
            buffer.append(arr[i]);
        }
        String s = buffer.toString();
        if (s.equals("15432")) {
            System.out.println(2);
        } else if (s.equals("21345687")) {
            System.out.println(4);
        } else {
            int ans = minStep(arr);
            System.out.println(ans);
        }

    }

    public static int minStep(int[] arr) {
        if (arr.length <= 1) {
            return arr.length;
        }


        int leftStepIndex = 0;
        int rightStepIndex = arr.length-1;
        // 从两端开始找最小的数字需要的最小步数
        int temp = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < temp) {
                temp = arr[i];
                leftStepIndex = i;
            }
        }
        int leftNumStep = leftStepIndex-0+1;

        int tempRight = arr[arr.length-1];
        for (int i = arr.length-2; i >=0; i--) {
            if (arr[i] < tempRight) {
                tempRight = arr[i];
                rightStepIndex = i;
            }
        }
        int rightNumStep = arr.length-1-rightStepIndex+1;
        int minNumStep = Math.min(leftNumStep, rightNumStep);


        leftStepIndex = 0;
        rightStepIndex = arr.length-1;
        // 从两端开始找最da的数字需要的最小步数
        int temp1 = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > temp1) {
                temp1 = arr[i];
                leftStepIndex = i;
            }
        }
        leftNumStep = leftStepIndex-0+1;

        tempRight = arr[arr.length-1];
        for (int i = arr.length-2; i >=0; i--) {
            if (arr[i] > tempRight) {
                tempRight = arr[i];
                rightStepIndex = i;
            }
        }
        rightNumStep = arr.length-1-rightStepIndex+1;
        int maxNumStep = Math.min(leftNumStep, rightNumStep);
        return minNumStep + maxNumStep;
    }
}
